<?php

namespace app\admin\controller\finance;

use app\common\controller\Backend;

/**
 * 收入预算
 *
 * @icon fa fa-circle-o
 */
class Incomebudget extends Backend
{

    /**
     * Incomebudget模型对象
     * @var \app\admin\model\finance\Incomebudget
     */
    protected $model = null;

    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'id,yer';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\finance\Incomebudget;
        $this->view->assign("mthList", $this->model->getMthList());
        //开启模型验证
        $this->modelValidate = true;
    }

    /**
     * 查看
     */
    public function index()
    {
        $admin = $this->auth->getUserInfo();
        $group = $this->auth->getGroups();
        $groupId = $group[0]["group_id"];
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            if ($groupId == 1) {
                $list = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
            } else if ($groupId == 2) {
                $list = $this->model
                    ->where($where)
                    ->where(['h_id' => $admin["home_id"]])
                    ->order($sort, $order)
                    ->paginate($limit);
            } else if ($groupId == 3) {
                $list = $this->model
                    ->where($where)
                    ->where(['u_id' => $admin["id"]])
                    ->order($sort, $order)
                    ->paginate($limit);
            }


            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $row) {
                $row->visible(['id', 'yer', 'mth', 'amount', 'createtime', 'updatetime']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }
}
